{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "84da2399",
   "metadata": {},
   "source": [
    "# 10.1 Coupling NGSolve and NGSpice"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d3248642",
   "metadata": {},
   "outputs": [],
   "source": [
    "from netgen.occ import *\n",
    "outer = MoveTo(0,0).RectangleC(0.1,0.1).Face()\n",
    "el1 = MoveTo(0,0.002).RectangleC(0.03,0.001).Face()\n",
    "el2 = MoveTo(0,-0.002).RectangleC(0.03,0.001).Face()\n",
    "el1.edges.name=\"el1\"\n",
    "el2.edges.name=\"el2\"\n",
    "\n",
    "dom = outer-el1-el2\n",
    "geom = OCCGeometry(dom, dim=2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f4e2ec45",
   "metadata": {},
   "outputs": [],
   "source": [
    "from ngsolve import *\n",
    "from ngsolve.webgui import Draw\n",
    "mesh = Mesh(geom.GenerateMesh(maxh=0.005))\n",
    "# Draw (mesh);"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6455acfe",
   "metadata": {},
   "outputs": [],
   "source": [
    "fes = H1(mesh, order=3, dirichlet=\"el1|el2\")\n",
    "u,v = fes.TnT()\n",
    "gfu = GridFunction(fes)\n",
    "gfu.Set (mesh.BoundaryCF({\"el1\":1, \"el2\":0}), BND)\n",
    "\n",
    "mata = BilinearForm(10*8.854e-12*grad(u)*grad(v)*dx).Assemble().mat\n",
    "inv = mata.Inverse(inverse=\"sparsecholesky\", freedofs=fes.FreeDofs())\n",
    "gfu.vec.data -= inv@mata * gfu.vec\n",
    "\n",
    "capacity = InnerProduct(mata*gfu.vec, gfu.vec)\n",
    "print (\"capacity=\", capacity)\n",
    "Draw (gfu);"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a77badcc",
   "metadata": {},
   "outputs": [],
   "source": [
    "try:\n",
    "    from PySpice.Spice.Netlist import Circuit\n",
    "    from PySpice.Unit import *\n",
    "except:\n",
    "    pass"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fb9fbf55",
   "metadata": {},
   "outputs": [],
   "source": [
    "try:\n",
    "    circuit = Circuit('test1')\n",
    "\n",
    "    source = circuit.SinusoidalVoltageSource(1, 'input', circuit.gnd, amplitude=1@u_V, frequency=1@u_kHz)\n",
    "    # source = circuit.PulseVoltageSource(1, 'input', circuit.gnd, 1, 2, 0, 0, 0, 0.001, 0.01)\n",
    "    \n",
    "    circuit.R(1, 'input', 'output', 100@u_kOhm)\n",
    "    # circuit.C(1, 'output', 0, 1@u_nF)\n",
    "    circuit.C(1, 'output', circuit.gnd, capacity)\n",
    "\n",
    "    print (circuit)\n",
    "except:\n",
    "    pass"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a10e71f7",
   "metadata": {},
   "outputs": [],
   "source": [
    "try:\n",
    "    simulator = circuit.simulator()\n",
    "    analysis = simulator.transient(step_time=source.period/50, end_time=source.period*4)\n",
    "    # analysis = simulator.transient(step_time=1e-4, end_time=1e-2)\n",
    "    \n",
    "    import matplotlib.pyplot as plt\n",
    "    plt.plot (analysis.input)\n",
    "    plt.plot (analysis.output)\n",
    "except:\n",
    "    pass"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d73e9d0f",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
